﻿using System;
using System.Collections.Generic;

namespace AppMain
{
    public class AttackEvent : BaseEvent<AttackData>
    {
        protected override void onProcess()
        {
            List<long> targets = new List<long>();
            var ier = RoleMgr.Instance.getMapEnumerator();

            while (ier.MoveNext())
            {
                IRole role = ier.Current.Value;
                if (role.AttrMap.getAttr<int>(RoleAttr.InvincibleCount).Value > 0)
                {
                    Console.WriteLine("无敌状态 无法受击");
                    continue;
                }
                if (role.AttrMap.getAttr<int>(RoleAttr.RoleType).Value == RoleType.Monster)
                {
                    Monster monster = role as Monster;
                    Search_Type searchType = (Search_Type)(m_data.searchType);
                    if (searchType == Search_Type.Circle)
                    {
                        float distance = Physics.getDistance(monster.box3D, this.Action.role.box3D);
                        if (distance <= this.m_data.sizeX)
                        {
                            targets.Add(monster.UID);
                        }
                    }
                    else if (searchType == Search_Type.Box)
                    {
                        Box3D hitBox = new Box3D(this.Action.role.box3D);
                        int flag = this.Action.role.AttrMap.getAttr<int>(RoleAttr.LookFlag).Value;
                        hitBox.posX += m_data.offsetX * flag;
                        hitBox.posY += m_data.offsetY;
                        hitBox.posZ += m_data.offsetZ;
                        hitBox.sizeX = m_data.sizeX;
                        hitBox.sizeY = m_data.sizeY;
                        hitBox.sizeZ = m_data.sizeZ;

                        if (Physics.cast3D(monster.box3D, hitBox))
                        {
                            targets.Add(monster.UID);
                        }
                        else
                        {
                            Console.WriteLine("没有击中");
                        }
                    }
                }
            }
            if (targets.Count > 0)
            {
                long roleId = this.Action.role.UID;
                int actionId = this.Action.id;
                int hitId = this.m_data.hitId;
                for (int i = 0; i < targets.Count; i++)
                {
                    EventMgr.Instance.dispatch(EventCmd.Role_Attack_Event, roleId, targets[i], actionId, hitId);
                }
            }
        }

    }

}
